const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const WxParse = require('../Common/wxParse/wxParse.js');

import F2 from '@antv/wx-f2'; // 注：也可以不引入， initChart 方法已经将 F2 传入，如果需要引入，注意需要安装 @antv/wx-f2 依赖

let chart = null;

var list = []
//数组排序
function compare(property, type) {
  return function (a, b) {
    var value1 = a[property];
    var value2 = b[property];
    let value;
    if (type == "mini") {
      value = value2 - value1;
    } else if (type == "max") {
      value = value1 - value2;
    }
    return value;
  };
}

function initChart(canvas, width, height, F2) { // 使用 F2 绘制图表
  var data = [
    { type: '平\n和', sales: Number(list[0]) },
    { type: '气\n虚', sales: Number(list[1]) },
    { type: '湿\n热', sales: Number(list[2]) },
    { type: '阴\n虚', sales: Number(list[3]) },
    { type: '气\n郁', sales: Number(list[4]) },
    { type: '血\n瘀', sales: Number(list[5]) },
    { type: '阳\n虚', sales: Number(list[6]) },
    { type: '痰\n湿', sales: Number(list[7]) },
    { type: '特\n禀', sales: Number(list[8]) },
    { type: '血\n虚', sales: Number(list[9]) },
    { type: '阴\n寒', sales: Number(list[10]) },
    { type: '阳\n热', sales: Number(list[11]) }
  ];
  data = data.sort(
    compare("sales", "max")
  );
  chart = new F2.Chart({
    el: canvas,
    width,
    height,
    pading: [0, 10, 0, 10]
  });

  chart.source(data, {
    sales: {
      tickCount: 5
    }
  });
  data.forEach((obj,index)=>{
    let color = '#fffe02'
    if (obj.sales <= 20 && obj.sales >= 0) {
      color = '#fffe02';
    } else if (obj.sales <= 40 & obj.sales > 20) {
      color = '#fea828';
    } else if (obj.sales <= 60 && obj.sales > 40) {
      color = '#92651f';
    } else if (obj.sales > 60) {
      color = '#f60400';
    }
    chart.guide().text({
      position: [obj.type, obj.sales > 0 ? obj.sales : 0],
      content: obj.sales,
      style: {
        fill: color,
        textBaseline: 'bottom',
        fontSize:'9'
      },
      offsetY: -5
    });
  })
  chart.tooltip(false);
  chart.axis('sales', {

    position: 'left', // 纵坐标位置
  })
  chart.axis('type', {
    label: {
      textAlign: 'center',
      textBaseline: 'top'
    }
  });
  chart.interval().position('type*sales').color('sales', function (val) {
    if (val <= 20 && val >= 0) {
      return '#fffe02';
    } else if (val <= 40 & val > 20) {
      return '#fea828';
    } else if (val <= 60 && val > 40) {
      return '#92651f';
    } else if (val > 60) {
      return '#f60400';
    }
  });;
  chart.render();
  return chart;
}

Page({
  data: {
    detail:'',
    id:'',
    userInfo:'',
    more:false,
    resProgram:'',
    resDesc:'',
    resTips:'',
    opts: null,
    cartdetail: null
  },
  onLoad: function (options) {
    this.myLastHealthReport()
    this.info()
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  onShow: function () {
    
  },
  examdetail() {
    Controller.examdetail({
      healthReportId: this.data.detail.id
    }).then(res => {
      if (res.code == 0) {
        let cartdetail = res.data.baseData
        list = cartdetail.scores.split(',')
        this.setData({
          cartdetail,
          opts: {
            onInit: initChart
          }
        })
      }
    })
  },
  golink(e){
    if (!wx.getStorageSync('logintoken')) {
      app.ToLogin(true)
      return
    }
    var url=e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },
  return(){
    wx.navigateTo({
      url: "/pages/report/report"
    })
  },
  info(){
    Controller.info().then(res=>{
      if(res.data.baseData){
        this.setData({
          userInfo:res.data.baseData
        })
      }
    })
  },
  myLastHealthReport(){
    Controller.myLastHealthReport().then(res=>{
      if(res.data.baseData && res.data.baseData.title){
        if(res.data.baseData.resProgram){
          res.data.baseData.resProgram = JSON.stringify(res.data.baseData.resProgram).replace(/<img/gi, "<img class='richImg' style='width:auto!important;height:auto!important;max-height:100%;width:100%;'");
          res.data.baseData.resProgram = JSON.parse(res.data.baseData.resProgram);
          var resProgram=res.data.baseData.resProgram
          var that = this;
          WxParse.wxParse('resProgram', 'html', resProgram, that, 5);
        }
        if(res.data.baseData.resDesc){
          res.data.baseData.resDesc = JSON.stringify(res.data.baseData.resDesc).replace(/<img/gi, "<img class='richImg' style='width:auto!important;height:auto!important;max-height:100%;width:100%;'");
          res.data.baseData.resDesc = JSON.parse(res.data.baseData.resDesc);
          var resDesc=res.data.baseData.resDesc
          var that = this;
          WxParse.wxParse('resDesc', 'html', resDesc, that, 5);
        }
        if(res.data.baseData.resTips){
          res.data.baseData.resTips = JSON.stringify(res.data.baseData.resTips).replace(/<img/gi, "<img class='richImg' style='width:auto!important;height:auto!important;max-height:100%;width:100%;'");
          res.data.baseData.resTips = JSON.parse(res.data.baseData.resTips);
          var resTips=res.data.baseData.resTips
          var that = this;
          WxParse.wxParse('resTips', 'html', resTips, that, 5);
        }

        this.setData({
          detail:res.data.baseData,
        })
        this.examdetail()
      }
    })
  },
  gofamily(){
    wx.navigateTo({
      url: "/pages/report/family"
    })
  },
  onShareAppMessage: function (e) {
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let title = '';
    let imageUrl = '';
    let path=app.getCurrentPageUrlWithArgs()
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },})